<?php namespace app\admin\controller;

use houdunwang\request\Request;
use houdunwang\route\Controller;
use system\model\Slide as SlideModel;

class Slide extends Common
{
    //动作

    public function __construct()
    {
        $this->auth();
    }

    public function index()
    {
        $field = SlideModel::paginate(3);
        //此处书写代码...
        return view('', compact('field'));
    }


    public function post()
    {
// 获得编辑的那个轮播图的id
        $slide_id = Request::get('slide_id');
//        查找是否有 没有则实例化
        $model = SlideModel::find($slide_id) ?: new SlideModel();
        if (IS_POST) {
//            存数据
            $post = Request::post();
            //yuantu
//            $post['slide_thumb']
            $filename='./'.rand(100,300).'.jpg';
            Image::thumb($post['slide_thumb'], $filename, 100, 100, 6);
            $post['slide_little_thumb'] = $filename;
//            echo "<img src='$filename'/>";

//            p($filename);die();
//            p($post);die;
            $model->save($post);

            return $this->setRedirect('index')->success('操作成功');
        }

        return view('', compact('model'));
    }

    public function remove()
    {
// get到id
        $slide_id = Request::get('slide_id');
//        找到id对应的数据
        $model = SlideModel::find($slide_id);
//        删除
        $model->destory();
        return $this->setRedirect('index')->success('操作成功');
    }

}
